﻿using MediatR;
using Medical.Api.Write.Application.Command.RecyclingCleaning;
using Medical.Api.Write.Application.Command.RecyclingCleaning.Disinfects;
using Medical.ErrorCode;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Medical.Api.Write.Controllers
{
    /// <summary>
    /// 回收清理管理--控制器(写)
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RecyclingCleaningController : ControllerBase
    {

        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<RecyclingCleaningController> logger;
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="mediator">中介者</param>
        public RecyclingCleaningController(ILogger<RecyclingCleaningController> logger, IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }
        /// <summary>
        /// 添加回收登记
        /// </summary>
        /// <returns>返回受影影响行数</returns>

        [HttpPost]
        public async Task<ApiResult<int>> AddRecoveryRegistration([FromBody]AddRecoveryRegistrationCommand command)
        {

            try
            {

                return await mediator.Send(command);

            }
            catch (Exception ex)
            {
                logger.LogError("添加回收登记异常处理"+ex.Message);
                throw;
            }

        }

        /// <summary>
        /// 修改回收状态
        /// </summary>
        /// <param name="command">修改回收状态命令</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdateRecoveryStatus([FromBody] UpdateRecoveryStatusCommand command)
        {
            try
            {
                logger.LogInformation($"修改回收ID为{command.Ids}的回收状态");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError($"修改回收状态异常处理: {ex.Message}");
                return new ApiResult<int>
                {
                    Code = ApiEnums.Fail,
                    Data = 0,
                    Msg = $"修改回收状态失败: {ex.Message}"
                };

            }
        }

        /// <summary>
        /// 撤销回收状态
        /// </summary>
        /// <param name="id">回收登记ID</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut("{id}")]
        public async Task<ApiResult<int>> CancelRecoveryStatus(int id)
        {
            try
            {
                logger.LogInformation($"撤销回收ID为{id}的回收状态");
                var command = new CancelRecoveryStatusCommand { Id = id };
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError($"撤销回收状态异常处理: {ex.Message}");
                return new ApiResult<int>
                {
                    Code = ApiEnums.Fail,
                    Data = 0,
                    Msg = $"撤销回收状态失败: {ex.Message}"
                };
            }
        }

        /// <summary>
        /// 添加清洗登记
        /// </summary>
        /// <param name="command">清洗登记命令</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddCleaningRegistration([FromBody] AddCleaningRegistrationCommand command)
        {
            try
            {
                logger.LogInformation("添加清洗登记");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError($"添加清洗登记异常处理: {ex.Message}");
                return new ApiResult<int>
                {
                    Code = ApiEnums.Fail,
                    Data = 0,
                    Msg = $"添加清洗登记失败: {ex.Message}"
                };
            }

        }

        /// <summary>
        /// 修改登记清洗状态为清洗完成
        /// </summary>
        /// <param name="command">修改登记清洗状态命令</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdateCleaningCompleted([FromBody] UpdateCleaningCompletedCommand command)
        {
            try
            {
                logger.LogInformation($"修改清洗登记ID为{command.Id}的清洗状态为已完成");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError($"修改清洗状态为已完成异常处理: {ex.Message}");
                return new ApiResult<int>
                {
                    Code = ApiEnums.Fail,
                    Data = 0,
                    Msg = $"修改清洗状态为已完成失败: {ex.Message}"
                };
            }
        }

        /// <summary>
        /// 逻辑删除清洗登记信息
        /// </summary>
        /// <param name="id">清洗登记ID</param>
        /// <returns>返回受影响行数</returns>
        [HttpDelete("{id}")]
        public async Task<ApiResult<int>> DeleteCleaningRegistration(int id)
        {
            try
            {
                logger.LogInformation($"逻辑删除清洗登记ID为{id}的信息");
                var command = new IsDelRegistrationCleaningCommand { Id = id };
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError($"逻辑删除清洗登记信息异常: {ex.Message}");
                return new ApiResult<int>
                {
                    Code = ApiEnums.Fail,
                    Data = 0,
                    Msg = $"逻辑删除清洗登记信息失败: {ex.Message}"
                };
            }
        }

        /// <summary>
        /// 修改清洗登记
        /// </summary>
        /// <param name="command">修改清洗登记命令</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdateCleaningRegistration([FromBody] UpdateCleaningRegistrationCommand command)
        {
            try
            {
                logger.LogInformation("修改清洗登记");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError($"修改清洗登记异常处理: {ex.Message}");
                return new ApiResult<int>
                {
                    Code = ApiEnums.Fail,
                    Data = 0,
                    Msg = $"修改清洗登记失败: {ex.Message}"
                };
            }
        }
        /// <summary>
        /// 撤销清洗状态
        /// </summary>
        /// <param name="command">撤销清洗状态命令</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> CancelCleaningState(CancelCleaningStateCommand command)
        {
            try
            {
                logger.LogInformation("撤销清洗状态");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError($"撤销清洗状态异常处理: {ex.Message}");
                return new ApiResult<int>
                {
                    Code = ApiEnums.Fail,
                    Data = 0,
                    Msg = $"撤销清洗状态失败: {ex.Message}"
                };
            }
        }

        /// <summary>
        /// 添加消毒登记信息
        /// </summary>
        /// <param name="command">消毒登记命令</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddDisinfection([FromBody]AddDisinfectionCommand command)
        {
            try
            {
                logger.LogInformation("添加消毒登记信息");
                var result = await mediator.Send(command);
                return result;
            }
            catch (System.Exception ex)
            {
                logger.LogError(ex, "添加消毒登记信息时发生异常");
                return new ApiResult<int>
                {
                    Code = ApiEnums.Error,
                    Msg = "添加消毒登记信息失败：" + ex.Message,
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 修改消毒状态
        /// </summary>
        /// <param name="command">修改消毒状态命令</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdateDisinfectionState([FromBody] UpdateDisinfectionStateCommand command)
        {
            try
            {
                logger.LogInformation($"修改消毒登记ID为{command.Id}的消毒状态为{command.State}");
                var result = await mediator.Send(command);
                return result;
            }
            catch (System.Exception ex)
            {
                logger.LogError(ex, "修改消毒状态时发生异常");
                return new ApiResult<int>
                {
                    Code = ApiEnums.Error,
                    Msg = "修改消毒状态失败：" + ex.Message,
                    Data = 0
                };
            }
        }

    }
}
